package com.ld.caodan.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

/**
 * 
 * 用来控制wifi的开关
 * 
 * @author 刘笛
 * 
 *         2011-6-10 下午05:15:18
 */
public class WifiAdmin {
    private WifiManager manager;
    private WifiInfo info;
    private static WifiAdmin admin = null;

    private WifiAdmin(Context context) {
	this.manager = (WifiManager) context
		.getSystemService(Context.WIFI_SERVICE);
	this.info = manager.getConnectionInfo();
    }

    public static WifiAdmin getInstance(Context context) {
	if (admin == null) {
	    return new WifiAdmin(context);
	}
	return admin;
    }

    /**
     * 打开wifi
     */
    public void openWifi() {
	if (!manager.isWifiEnabled()) {
	    manager.setWifiEnabled(true);
	}
    }

    /**
     * 关闭wifi
     */
    public void closeWifi() {
	if (manager.isWifiEnabled()) {
	    manager.setWifiEnabled(false);
	}
    }

    /**
     * 获取wifiinfo
     * 
     * @return
     */
    public WifiInfo getWifiInfo() {
	return info;
    }

    /**
     * 获取连接的id
     * 
     * @return
     */
    public int getNetWorkId() {
	return info == null ? 0 : info.getNetworkId();
    }
}
